import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
  RouteRecordRaw,
} from 'vue-router';

import Home from '../views/Home.vue';

import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

import store from '@store/index';

// 基础路由
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: Home,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('@views/Common/Home/HomePage.vue'),
      },
      {
        path: '/login',
        component: () => import('@views/Common/Login/Login.vue'),
      },
      {
        path: '/q',
        component: () => import('@views/Questionnaire/Question.vue'),
        redirect: '/q/list',
        children: [
          {
            path: '/q/a/:type/:id',
            component: () =>
              import('@/views/Questionnaire/QuestionAfter/QuestionAfter.vue'),
            redirect: '/q/a/:type/:id/design',
            props: true,
            children: [
              {
                name: 'send',
                path: '/q/a/:type/:id/send',
                component: () =>
                  import(
                    '@/views/Questionnaire/QuestionAfter/QuestionSend/QuestionSend.vue'
                  ),
                props: true,
              },
              {
                name: 'design',
                path: '/q/a/:type/:id/design',
                component: () =>
                  import(
                    '@/views/Questionnaire/QuestionAfter/QuestionDesign/QuestionDesign.vue'
                  ),
                props: true,
              },
              {
                path: '/q/a/:type/:id/overview',
                component: () =>
                  import(
                    '@/views/Questionnaire/QuestionAfter/QuestionOverview/QuestionOverview.vue'
                  ),
                props: true,
              },
              {
                path: '/q/a/:type/:id/analysis',
                component: () =>
                  import(
                    '@/views/Questionnaire/QuestionAfter/QuestionAnalysis/QuestionAnalysis.vue'
                  ),
                props: true,
              },
              {
                path: '/q/a/:type/:id/analysis_x',
                component: () =>
                  import(
                    '@/views/Questionnaire/QuestionAfter/QuestionAnalysisX/QuestionAnalysisX.vue'
                  ),
                props: true,
              },
            ],
          },

          {
            path: '/q/create/:type/:id',
            // path: '/q/create',
            component: () =>
              import('@views/Questionnaire/QuestionCreate/QuestionCreate.vue'),
            props: true,
          },
          {
            path: '/q/list',
            component: () =>
              import('@views/Questionnaire/QuestionList/QuestionMenu.vue'),
            redirect: '/q/list/present',
            children: [
              {
                path: '/q/list/present',
                component: () =>
                  import(
                    '@views/Questionnaire/QuestionList/QuestionList/QuestionList.vue'
                  ),
              },
              {
                path: '/q/list/recycle',
                component: () =>
                  import(
                    '@views/Questionnaire/QuestionList/RecycleBin/RecycleBin.vue'
                  ),
              },
            ],
          },
        ],
      },
      {
        path: '/r/:id/:code',
        component: () =>
          import('@views/Public/QuestionRelease/QuestionRelease.vue'),
        props: true,
      },
      {
        name: 'preview',
        path: '/q/preview',
        component: () =>
          import('@views/Questionnaire/QuestionPreview/QuestionPreview.vue'),
      },
      {
        path: '/r/result/:id/:code',
        component: () => import('@views/Public/Result/Result.vue'),
        props: true,
      },
      {
        path: '/r/error',
        component: () => import('@views/Public/Error/ErrorCode.vue'),
      },
    ],
  },
  {
    path: '/:catchAll(.*)',
    name: '404',
    component: () => import('@views/Public/Error/404.vue'),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      // 通过前进后台时才触发
      return savedPosition;
    } else {
      return { top: 0, behavior: 'smooth' };
    }
  },
});

// 访问控制
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    if (store.state.auth) {
      next('/home');
    } else {
      NProgress.start();
      next();
    }
  } else if (to.path.indexOf('/q/') >= 0) {
    if (store.state.auth) {
      NProgress.start();
      next();
    } else {
      next('/login');
    }
  } else {
    NProgress.start();
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
